package cn.yunhe.controller;

import cn.yunhe.model.PageParams;
import cn.yunhe.model.Role;
import cn.yunhe.model.RoleParams;
import cn.yunhe.view.ExcelExportService;
import cn.yunhe.view.ExcelView;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zs on 2017/12/6.
 */

@Controller
@RequestMapping("/role")
public class RoleController {

    @RequestMapping("/showRoleJsonInfo")
    public ModelAndView showRoleJsonInfo(Long id, String roleName, String note) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("id", id);
        mv.addObject("roleName", roleName);
        mv.addObject("note", note);
        return mv;

    }

    /**
     * 通过字符串实现重定向
     *
     * @param model
     * @param roleName
     * @param note
     * @return
     */
    @RequestMapping("/addRole")
    public String addRole(Model model, String roleName, String note) {
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleName(note);

        model.addAttribute("roleName", roleName);
        model.addAttribute("note", note);
        model.addAttribute("id", 8);

        return "redirect:/role/showRoleJsonInfo.do";
    }


    @RequestMapping("/addRole2")
    public ModelAndView addRole2(Model model, String roleName, String note) {
        ModelAndView mv = new ModelAndView();

        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleName(note);

        mv.addObject("roleName", roleName);
        mv.addObject("note", note);
        mv.addObject("id", 8);

        mv.setViewName("redirect:/role/showRoleJsonInfo.do");
        return mv;
    }

    /**
     * 通过POJO转换为json视图
     *
     * @param role
     * @return
     */
    @RequestMapping("/showRoleJsonInfo2")
    public ModelAndView showRoleJsonInfo(Role role) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("role", role);
        return mv;
    }

    /**
     * 重定向传递POJO
     *
     * @param ra
     * @param role
     * @return
     */
    @RequestMapping("/addRole3")
    public String addRole3(RedirectAttributes ra, Role role) {
        ra.addFlashAttribute("role", role);
        return "redirect:/role/showRoleJsonInfo2.do";
    }


    @RequestMapping(value = "/getRoleByModelMap", method = RequestMethod.GET)
    public ModelAndView getRoleByModelMap(@RequestParam("id") Long id, ModelMap modelMap) {
        Role role = new Role();
        role.setRoleName("孙二娘");
        role.setRoleName("母夜叉");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("roleDetails");
        modelMap.addAttribute("role", role);
        return mv;
    }

    @RequestMapping(value = "/getRoleByModel", method = RequestMethod.GET)
    public ModelAndView getRoleByModel(@RequestParam("id") Long id, Model model) {
        Role role = new Role();
        role.setRoleName("张青");
        role.setRoleName("菜园子");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("roleDetails");
        model.addAttribute("role", role);
        return mv;
    }


    @RequestMapping(value = "/getRoleByMv", method = RequestMethod.GET)
    public ModelAndView getRoleByMv(@RequestParam("id") Long id, ModelAndView mv) {
        Role role = new Role();
        role.setRoleName("武松");
        role.setRoleName("行者");
        mv.setViewName("roleDetails");
        mv.addObject("role", role);
        return mv;
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export() {
        //模型和视图
        ModelAndView mv = new ModelAndView();
        //Excel视图，并设置自定义导出接口
        ExcelView ev = new ExcelView(exportService());
        //文件名
        ev.setFileName("所有角色.xls");
        //设置sql后台参数
        RoleParams roleParams = new RoleParams();
        //限制1万条
        PageParams page = new PageParams();
        page.setStart(0);
        page.setLimit(10000);
        roleParams.setPageParams(page);
        //数据，构建对象，省略数据库步骤
        Role role1 = new Role();
        role1.setId(1L);
        role1.setRoleName("武松");
        role1.setNote("行者");


        Role role2 = new Role();
        role2.setId(2L);
        role2.setRoleName("鲁智深");
        role2.setNote("花和尚");

        Role role3 = new Role();
        role3.setId(3L);
        role3.setRoleName("杨志");
        role3.setNote("青面兽");

        List<Role> roleList = new ArrayList<Role>();
        roleList.add(role1);
        roleList.add(role2);
        roleList.add(role3);
        //加入数据模型
        mv.addObject("roleList", roleList);
        mv.setView(ev);
        return mv;


    }

    @SuppressWarnings({"unchecked"})
    private ExcelExportService exportService() {
        //使用Lambda表达式自定义导出excel规则
        return (Map<String, Object> model, Workbook workbook) -> {
            List<Role> roleList = (List<Role>) model.get("roleList");
            Sheet sheet = workbook.createSheet("所有角色");
            Row title = sheet.createRow(0);
            title.createCell(0).setCellValue("编号");
            title.createCell(1).setCellValue("姓名");
            title.createCell(2).setCellValue("备注");

            //遍历列表，生成数据
            for (int i = 0; i < roleList.size(); i++) {
                Role role = roleList.get(i);
                int rowIdx = i + 1;
                Row row = sheet.createRow(rowIdx);
                row.createCell(0).setCellValue(role.getId());
                row.createCell(1).setCellValue(role.getRoleName());
                row.createCell(2).setCellValue(role.getNote());
            }


            /**/


        };
    }


}
